<?php
class ZoneEditForm extends Form{
	private $cat_zone=false;//,$cats = array();
	private $cats = '';
	function ZoneEditForm(){
		Form::Form('ZoneEditForm');
		
		if(Url::get('cmd')=='edit'){
			CGlobal::$website_title='Sửa Nhóm Danh mục';
			$id=(int)Url::get('id',0);
			
			if($id)$this->cat_zone=DB::select('category_zone','id='.$id);
			
			if(!$this->cat_zone){
				Url::redirect_current();
			}
			
			//$re = DB::query("SELECT c.id, c.name FROM (SELECT zoneid, catid FROM category_zone_cat WHERE zoneid=$id) AS z LEFT JOIN category AS c ON c.id=z.catid");
			$re = DB::query("SELECT catid FROM category_zone_cat WHERE zoneid=$id ORDER BY position");
			if($re){
				while($cat = mysql_fetch_assoc($re)){
					//$this->cats[$cat['id']] = $cat;	
					if($this->cats != '') $this->cats.=',';
					$this->cats.=$cat['catid'];
				}
			}
		}
		else{
			CGlobal::$website_title='Thêm Nhóm Danh mục';
			
			$this->cat_zone=array(	
							'name'			=>'',
							'brief_name'	=>'',
							'position'		=>1,
							'status'		=>0,
							'class'			=>'fashion'
							); 
		}
	}
	
	function on_submit(){
		$name		=Url::get('name');
		$brief_name	=Url::get('brief_name');
		$status		=(int)Url::get('status','HIDE');
		$position	=(int)Url::get('position');
		$class		=Url::get('class');
		$catids		=Url::get('catids');
		
		$this->checkFormInput('Tên Danh mục','name',$name,'str',true,'',1,255);
		$this->checkFormInput('Tên vắn tắt','brief_name',$brief_name,'str',false,'',1,255);
		//$this->checkFormInput('Mô tả','description',$description,'str',false,'',0,250);
		
		if(!$this->errNum){
			$new_row =array(	
							'name'			=>$name,
							'brief_name'	=>$brief_name?$brief_name:$name,
							'status'		=>$status,
							'class'			=>$class,
							);
							
			$update_orther_pos=false;
								
			if(Url::get('cmd')=='edit'){
				if($position>0 && $position != $this->cat_zone['position']){
					$max_pos=(int)DB::fetch("SELECT max(position) AS max_pos FROM category_zone",'max_pos');
					if($position>$max_pos){
						$position=$max_pos+1;
					}
					$new_row['position']=$position;
				}
				else{
					$position = $this->cat_zone['position'];
				}
				
				
				$id=$this->cat_zone['id'];
				DB::update('category_zone', $new_row,'id='.$this->cat_zone['id']);
			}
			else{
				$max_pos=(int)DB::fetch("SELECT max(position) AS max_pos FROM category_zone",'max_pos');
				
				if($position>$max_pos || $position==0) 
				$position=$max_pos+1;
				
				$new_row['position']=$position;
				
				$id = DB::insert('category_zone', $new_row);
			}
			
			if($id && DB::select('category_zone',"position=$position AND id!=$id")){
				DB::query("UPDATE category_zone SET position=position+1 WHERE position>=$position AND id!=$id");
			}
			
			$all_cats  = $this->cats;
			$sql_inset = "";
			$position = 0;
			$arrCatIDs = split(',',$catids);
			foreach ($arrCatIDs as $catid){
				//if(isset($all_cats[$catid])){
				//	unset($all_cats[$catid]);
				//}
				//else{
					$position++;
					$sql_inset.=($sql_inset!=''?',':'')."($id,$catid,$position)";
				//}
			}
			
			if($all_cats){
				//$catids = implode(",",array_keys($all_cats));
				
				//if($catids){
					//echo "DELETE FROM category_zone_cat WHERE zoneid=$id AND catid IN($all_cats)";
					DB::query("DELETE FROM category_zone_cat WHERE zoneid=$id AND catid IN($all_cats)");
				//}
			}
			
			if($sql_inset){
				$sql_inset="INSERT INTO category_zone_cat (zoneid, catid,position) VALUES ".$sql_inset;
				//echo $sql_inset;
				DB::query($sql_inset);
			}
			
			//AZLib::del_cache_zone_cat();
			
			AZLib::getCats(1,true);
			//Url::redirect_current(array('mod'));
		}
	}
	
	function draw(){	
		global $display;
		$this->beginForm(true);
		
		$display->add('msg',$this->showFormErrorMessages(true));
		
		$re=DB::query("SELECT id,name, status FROM category WHERE parent_id=0 ORDER BY position");
		
		$all_top_cat=array();
		if($re){
			/*while($cat=mysql_fetch_assoc($re)){
				if($cat['status'] == 'HOME'){
					$cat['name'].=' (HOME)';
				}
				elseif($cat['status'] == 'HIDE'){
					$cat['name'].=' (HIDE)';
				}
				
				$all_top_cat[$cat['id']]=$cat['name'];
			}*/
			while($cat=mysql_fetch_assoc($re)){
				$all_top_cat[$cat['id']]=$cat;
			}
		}
		$display->add('all_top_cat',$all_top_cat);
		//$display->add('option_cat_ids',AZLib::getOptionMulti($all_top_cat,Url::get('catids',array_keys($this->cats))));
		$id =(int)Url::get('id',0); 
		$re1=DB::query('SELECT category.id,name, status,category_zone_cat.position FROM category LEFT JOIN category_zone_cat ON category.id = category_zone_cat.catid WHERE category_zone_cat.zoneid='.$id.' ORDER BY category_zone_cat.position');
		$items = array();
		if($re1)
		{
			$last_id=0;
			while($row = mysql_fetch_assoc($re1))
			{
				/*if($row['status'] == 1){
					$row['status']='<b>Hiện</b>';
				}
				else{
					$row['status']='<b>Ẩn</b>';
				}
				if($last_id){
					$items[$last_id]['down']='<a href="'.Url::build_current(array('mod','cmd'=>'move_zonecat_down','catid'=>$last_id,'id'=>$id)).'"><img src="style/images/admin/down_arrow_.gif" /></a>';
					$row['up']='<a href="'.Url::build_current(array('mod','cmd'=>'move_zonecat_up','catid'=>$row['id'],'id'=>$id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
				}
				else {
					$row['up']='';
				}
				$row['down']='';*/
				
				$items[$row['id']]=$row;
				$last_id = $row['id'];
			}
			/*if($last_id && $items[$last_id]['position']>1){
				$items[$last_id]['up']='<a href="'.Url::build_current(array('mod','cmd'=>'move_zonecat_up','catid'=>$last_id,'id'=>$id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
			}*/
		}
		$display->add('items',$items);
		$display->add('catids',Url::get('catids',$this->cats));
		$display->add('name',Url::get('name',$this->cat_zone['name']));
		$display->add('brief_name',Url::get('brief_name',$this->cat_zone['brief_name']));
		$display->add('option_status',AZLib::getOption(array(0=>'Ẩn',1=>'Hiện'),Url::get('status',$this->cat_zone['status'])));
		$display->add('option_position',AZLib::getOptionNum(1,100,Url::get('position',$this->cat_zone['position'])));
		$display->add('class',Url::get('class',$this->cat_zone['class']));
		
		
		$display->output('zone_edit');
		$this->endForm();
	}
}
?>